package com.example.ying.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.ying.remote.HelloRemote;

@RestController
@RefreshScope
public class HelloWorldController {
	
    @Value("${neo.hi}")
    private String hi;

    @RequestMapping("/hi")
    public String hi() {
        return this.hi;
    }
    
	@Autowired
	HelloRemote helloRemote;
    @RequestMapping("/hello")
    public String index(String name) {
    	if (name==null){
    		name="ying";
    	}
        return helloRemote.hello(name);
    }
    
    @Value("${server.port}")
    private String port;
    
    @RequestMapping(value = "/first", method = RequestMethod.GET)  
    public Map<String, Object> firstResp (HttpServletRequest request){  
        Map<String, Object> map = new HashMap<>();  
        request.getSession().setAttribute("port", port);  
        map.put("port", port);  
        return map;  
    }  
  
    @RequestMapping(value = "/sessions", method = RequestMethod.GET)  
    public Object sessions (HttpServletRequest request){  
        Map<String, Object> map = new HashMap<>();  
        map.put("sessionId", request.getSession().getId());  
        map.put("port", request.getSession().getAttribute("port"));  
        return map;  
    } 
}